<template>
  <div>
    <!-- 审批弹窗 -->
    <gzhb-dialog
      :visible.sync="isShowSubmitDialog"
      title="申请完成">
      <gzhb-field
        label-width="26%"
        label="提示：">
        <van-field 
          slot="input" 
          rows="1"
          readonly 
          autosize 
          v-model="SubmitDialogForm.tip" 
          type="textarea" 
          placeholder="" />
      </gzhb-field>
      <gzhb-field
        label-width="26%"
        label="完成说明：">
        <van-field 
          slot="input"
          rows="1" 
          autosize 
          readonly 
          v-model="SubmitDialogForm.description" 
          type="textarea" 
          placeholder="" />
      </gzhb-field>
      <gzhb-field
        label-width="26%"
        label="审批意见：">
        <van-field 
          slot="input" 
          v-model="SubmitDialogForm.comment" 
          rows="1" 
          type="textarea" 
          placeholder="请输入" 
          autosize />
      </gzhb-field>
      <gzhb-field
        v-if="SubmitStatusFrom.state === 'delay'"
        label-width="26%"
        label="延期时间：">
        <van-field 
          slot="input" 
          v-model="SubmitDialogForm.time" 
          rows="1"
          type="textarea" 
          placeholder="" 
          autosize 
          readonly
          @click="handleShowSubmitTimepopup" />
      </gzhb-field>
      <gzhb-field
        label-width="15%"
        label="">
        <div slot="input" class="ui-cell">
          <gzhb-radio v-model="SubmitDialogForm.status" label="同意">同意</gzhb-radio>
          <gzhb-radio v-model="SubmitDialogForm.status" label="不同意">不同意</gzhb-radio>
        </div>
      </gzhb-field>

      <!-- 按钮 -->
      <div class="ui-row ui-border__top">
        <div class="ui-col ui-col4 ui-col__offset4">
          <div class="ui-cell">
            <button @click="handleSubmit" class="ui-button ui-whole-line">确定</button>
          </div>
        </div>
      </div>
    </gzhb-dialog>

    <!-- 时间选择弹框 -->
    <van-popup
      v-model="SubmitDialogForm.show"
      position="bottom">
      <van-datetime-picker
        type="date"
        v-model="SubmitDialogForm.popupTime"
        @cancel="SubmitDialogForm.show = false"
        @confirm="pickTimeConfirm"/>
    </van-popup>

    <!-- loading -->
    <gzhb-loading :isShow="isLoading"></gzhb-loading>
  </div>
</template>

<script>
  import axios from '@/axios'
  import moment from 'moment'
  import { taskManageURL }  from '@/config.js'
  import { downloadFile, utils_fileIcon, utils_fileType, utils_fileSubmitTyp }  from '@/utils/index.js'
  export default {
    name: 'Shenpi',
    props: {
      isShowSubmitDialog:{
        type: Boolean,
        default: false
      },
      isLoading:{
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        // 申请完成弹框
        isShowSubmitDialog: false,        
        SubmitDialogForm: {
          tip: '',
          id: '',
          taskId: '',
          description: '',
          reportContent: '',
          status: '同意',
          popupTime: '',
          time: '',
          comment: '',
          show: false,
          obj: {}
        },
        SubmitStatusFrom: {
          state: '',
          compPct: ''
        }
      }
    },
    computed: {
      isShowBtn(){
        return this.page === '2' || this.page === 2
      }
    },
    created() {  
      this.page = this.$route.query.page
      this.SubmitStatusFrom = JSON.parse(localStorage.getItem('SubmitStatusFrom'))
      this.SubmitDialogForm = JSON.parse(localStorage.getItem('SubmitDialogForm'))
    },
    methods: {
      // 处理提交弹框的提交按钮事件
      handleSubmit(){
        let submitUrl = ''
        if( this.SubmitStatusFrom.state === 'finish' && this.SubmitDialogForm.status === '同意' ){
          // 申请完成同意
          submitUrl = 'taskProgressReportFinish'
        } else if( this.SubmitStatusFrom.state === 'finish' && this.SubmitDialogForm.status === '不同意' ){
          // 申请完成不同意
          submitUrl = 'taskProgressReportDisagree'
        } else if( this.SubmitStatusFrom.state === 'delay' && this.SubmitDialogForm.status === '同意' ){
          // 延期申请同意
          submitUrl = 'taskApplyDelayFinish'
        } else if(this.SubmitStatusFrom.state === 'delay' && this.SubmitDialogForm.status === '不同意'){
          // 延期申请不同意
          submitUrl = 'taskApplyDelayDisagree'
        } else if(this.SubmitStatusFrom.state === 'relay' && this.SubmitDialogForm.status === '同意'){
          // 转交申请同意
          submitUrl = 'taskApplyRelayAgree'
        } else if(this.SubmitStatusFrom.state === 'relay' && this.SubmitDialogForm.status === '不同意'){
          // 转交申请不同意
          submitUrl = 'taskApplyRelayDisagree'
        } else if(this.SubmitStatusFrom.state === 'terminate' && this.SubmitDialogForm.status === '同意'){
          // 终止申请同意
          submitUrl = 'terminateAgree'
        } else if(this.SubmitStatusFrom.state === 'terminate' && this.SubmitDialogForm.status === '不同意'){
          // 终止申请不同意
          submitUrl = 'terminateDisagree'
        }

        axios.post( submitUrl, {
          id: this.SubmitDialogForm.id,
          task: this.SubmitDialogForm.taskId,
          rejectDesc: this.SubmitDialogForm.comment, 
          newFinishDate: this.SubmitDialogForm.time
        }).then(res => {                      
          if(res.succ){
            this.$toast.success(res.msg || '审批成功')
            this.isShowSubmitDialog = false
          } else {
            this.$toast.fail(res.msg || '审批失败')
          }
        })
      },
      // 显示时间弹窗
      handleShowSubmitTimepopup(){
        this.SubmitDialogForm.show = true
      },
      // 选择弹框的时间
      pickTimeConfirm() {        
        this.SubmitDialogForm.time = moment(this.SubmitDialogForm.popupTime).format('YYYY-MM-DD')
        this.SubmitDialogForm.show = false
      }
    }
  }
</script>